It is often useful to represent a text as a list of tokens.
The process of breaking a text up into its constituent tokens is known
as tokenization. The subroutine parses the string in input into arguments
args(1), ..., args(nargs) based on the delimiters contained in the string
delims
. Preceding a delimiter in string
by a backslash \
makes this
particular instance not a delimiter.
The integer output variable nArgs contains the number of arguments found.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | string | |||
character(len=*), | intent(in) | :: | delims | |||
character(len=*), | POINTER | :: | args(:) |
local copy of string to tokenize |
||
integer(kind=short), | intent(out) | :: | nArgs |
Type | Visibility | Attributes | Name | Initial | |||
---|---|---|---|---|---|---|---|
integer(kind=short), | public | :: | i | ||||
integer(kind=short), | public | :: | na | ||||
character(len=LEN_TRIM), | public | :: | strSav |
SUBROUTINE StringTokenize & ! (string, delims, args, nArgs) ! Declarations: IMPLICIT NONE ! Subroutine arguments ! Scalar arguments with intent(in): CHARACTER(LEN = *), INTENT(IN) :: string CHARACTER(LEN = *), INTENT(IN) :: delims ! Scalar arguments with intent(out): INTEGER (KIND = short), INTENT(OUT) :: nArgs ! Array arguments with intent(out): CHARACTER (len=*), POINTER :: args(:) ! Local scalars: !! local copy of string to tokenize CHARACTER (LEN = LEN_TRIM(string)) :: strSav INTEGER (KIND = short) :: na INTEGER (KIND = short) :: i !------------end of declaration------------------------------------------------ strSav = StringCompact (string) IF ( LEN_TRIM (strSav) == 0 ) RETURN !string is empty ! Count number of tokens in string nArgs = 0 DO i = 1, LEN_TRIM(strSav) IF ( INDEX ( delims,strSav(i:i) ) > 0 ) THEN !the character is a delimiter nArgs = nArgs + 1 END IF END DO nArgs = nArgs + 1 !number of tokens are number of found delimiters + 1 !allocate space for tokens ALLOCATE ( args(nArgs) ) !initialize tokens DO i = 1,nArgs args(i) = ' ' END DO na = 0 DO IF (LEN_TRIM(strSav) == 0) EXIT na = na + 1 CALL StringSplit(delims,strSav,args(na)) END DO END SUBROUTINE StringTokenize